//
//  HJNewFeatureViewController.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/8.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit

class HJNewFeatureViewController: UIViewController,UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    
    private func setupUI() {
        
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        
        let count = 4
        
        for i in 0..<count {
            
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.bounds.width, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height)
            
            scrollView.addSubview(imageView)
            
            if i == count-1 {
                
               setupLastPage(imageView)
            }
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count) * scrollView.bounds.width, height: 0)
        
        pageControl.numberOfPages = count
        pageControl.frame = CGRect(x: scrollView.frame.width*0.5, y: scrollView.frame.height - 100, width: 0, height: 0 )
        
    }
    
    // 设置最后一页的内容
    
    private func setupLastPage(lastPage:UIView) {
        
        lastPage.userInteractionEnabled = true
        
        enterButton.center = CGPoint(x: lastPage.frame.width * 0.5, y: lastPage.frame.height - 150)
        shareButton.center = CGPoint(x: enterButton.center.x
            , y: CGRectGetMinY(enterButton.frame)-30)
        
        lastPage.addSubview(enterButton)
        lastPage.addSubview(shareButton)
    }
    
    
    // MARK: - UIScrollViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        
        pageControl.currentPage = Int(page)
    }
    
    // MARK: - 懒加载控件
    private lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.delegate = self
        return scrollView
    }()
    
    //分页控件
    private lazy var pageControl:UIPageControl = {
        
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.grayColor()
        return pageControl
    }()
    //进入微博
    private lazy var enterButton:UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setTitle("进入微博", forState: .Normal)
        
        button.sizeToFit()
        
        return button
    }()
    
    //分享按钮
    private lazy var shareButton:UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        button.setTitle("分享到微博", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.sizeToFit()
        
        return button
        
    }()
    
    // MARK: - 监听点击
    @objc private func enterButtonClick() {
        
        NSNotificationCenter.defaultCenter().postNotificationName("HMSwitchRootVCNotifation", object: self)
        
    }
    
    @objc private func sharedButtonClick(button:UIButton) {
        
        button.selected = !button.selected
    }

    
    
    
}
